//swingdoor.txt - A door.
//Cell 0 - If > 0, this door is locked.
//Cell 1 - The required key. If 0, none. If 99, the sdf is actually a blocking flag
//Cell 2,3 - The sdf which is set to 1 when door is unlocked.
//Cell 4 - If 1, start door as open
//Cell 5 - If > 0, plays this dialogue when tries to open if don't have the key.

beginobjectscript; // door lever

variables;
	short door_is_open = 0;
	short i_am_unlocked = 0;
	short init_open = 0;
	short give_xp = 0;
body;


beginstate INIT_STATE;
	if (get_memory_cell(0) == 0)
		i_am_unlocked = 1;

	if (get_memory_cell(4) > 0) {
		init_open = 1;
		set_object_blockage(ME,0);
		give_object_message(ME,103);
		}
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; // door closed, waiting
	if ((door_is_open == 0) && ((my_current_message() == 103) || (init_open > 0))) {
		init_open = 0;
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
			set_flag(get_memory_cell(2),get_memory_cell(3),1);
		
		door_is_open = 1;
		set_object_blockage(ME,0);

		}
	if ((door_is_open == 1) && (my_current_message() == 104)) { // close door mess
		if (creature_on_obj(ME) >= 0)
			end();
		door_is_open = 0;
		set_object_blockage(ME,3);

		}
break;

beginstate USE_STATE;
	//QW2 kludge for tutorial door
	if ((gf(8,13) == 0) && (party_has_item_equip(36))) {
		sf(8,13,1);
		sf(88,1,2);
		}

	if ((get_memory_cell(1) == 99) && (get_sdf(get_memory_cell(2),get_memory_cell(3)) == 0)) {
		begin_talk_mode(get_memory_cell(5));
		end();
		}
		
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			i_am_unlocked = 1;
		}

	if ((get_memory_cell(1) == 0) && (get_memory_cell(1) < 99) && (i_am_unlocked == 0)) {
		run_text_on_obj(ME,"Lock!",0);
		if (get_memory_cell(5) > 0)
					begin_talk_mode(get_memory_cell(5));
					else print_str_color("The door is locked, and you don't have the key.",1);
		end();
		}
	if ((get_memory_cell(1) > 0) && (get_memory_cell(1) < 99) && (i_am_unlocked == 0)) {
		if (has_spec_item(get_memory_cell(1))) {
			print_str_color("Locked Door: You use a key to unlock the door.",2);
			run_text_on_obj(ME,"Unlocked",0);
			i_am_unlocked = 1;
			play_sound(16);
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) 
				sf(get_memory_cell(2),get_memory_cell(3),1);
			}
			else {
				run_text_on_obj(ME,"Lock!",0);
				if (get_memory_cell(5) > 0)
					begin_talk_mode(get_memory_cell(5));
					else print_str_color("The door is locked, and you don't have the key.",1);

				}
		end();
		}
		else {
			pc_heard_sound(25 + (which_town() % 3));
			broadcast_object_message(1,103);
			
			}

break;
